package com.iflysse.services.impl;

import com.iflysse.common.ActivityState;
import com.iflysse.common.ResultEnum;
import com.iflysse.dao.TaskDao;
import com.iflysse.dao.TeachingActivityDao;
import com.iflysse.dao.TestDao;
import com.iflysse.pojo.bo.CommonBoResp;
import com.iflysse.pojo.bo.req.ActivityIdBoReq;
import com.iflysse.pojo.bo.req.ActivityStateBoReq;
import com.iflysse.pojo.bo.req.TeachingClassIdBoReq;
import com.iflysse.pojo.bo.req.TestBoReq;
import com.iflysse.pojo.bo.resp.TeachingActivityBoResp;
import com.iflysse.pojo.bo.resp.TeachingActivityDetailBoResp;
import com.iflysse.pojo.bo.resp.TeachingActivityListBoResp;
import com.iflysse.pojo.po.common.CommonPoResp;
import com.iflysse.pojo.po.req.TaskStatePoReq;
import com.iflysse.pojo.po.req.TeachingClassIdPoReq;
import com.iflysse.pojo.po.req.TestPoReq;
import com.iflysse.pojo.po.req.TestStatePoReq;
import com.iflysse.pojo.po.resp.TeachingActivityPoResp;
import com.iflysse.services.ActivityService;
import com.iflysse.util.CommonUtil;
import com.iflysse.util.LogWriterUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Service
public class ActivityServiceImpl implements ActivityService {
    public static final Logger log = Logger.getLogger(ActivityServiceImpl.class);

    @Autowired
    private TestDao testDao;
    @Autowired
    private TaskDao taskDao;
    @Autowired
    private TeachingActivityDao teachingActivityDao;
    /**
     * 暂存或者发布测试
     *
     * @param testBoReq
     * @return
     */
    @Transactional
    @Override
    public CommonBoResp saveOrPublishTest(TestBoReq testBoReq) throws SQLException {
        TestPoReq testPoReq = CommonUtil.objToObj(testBoReq, TestPoReq.class);
        testPoReq.setTestId(UUID.randomUUID().toString());
        CommonBoResp commonBoResp = new CommonBoResp();
        //创建结果对象,添加状态码
        CommonPoResp commonPoResp = new CommonPoResp();
        //创建结果对象,添加状态码
        CommonPoResp commonPoResp1 = new CommonPoResp();
            //添加测试
            int i = testDao.addTest(testPoReq);
            int x = testDao.addQuestionToTest(testPoReq);
            CommonUtil.printToPoResult(commonPoResp, i);
            CommonUtil.printToPoResult(commonPoResp1, x);
            int result = 0;
            if(commonPoResp.getCode() == ResultEnum.SUCCESS.getCode() && commonPoResp1.getCode() == ResultEnum.SUCCESS.getCode()){
                result = 1;
            }
            Map<String,Object> logData =new HashMap<>();
            logData.put("请求数据",testBoReq);
            logData.put("添加测试结果",commonPoResp);
            logData.put("添加题目到测试结果",commonPoResp1);
            LogWriterUtil.write(log,logData);
        return commonBoResp;
    }

    /**
     * 根据教学班级编号查询教学班级活动列表
     *
     * @param teachingClassIdBoReq
     * @return
     */
    @Override
    public TeachingActivityListBoResp loadTeachiActivityListByTeachingClassId(TeachingClassIdBoReq teachingClassIdBoReq) throws SQLException {
        //创建返回对象
        TeachingActivityListBoResp teachingActivityListBoResp = new TeachingActivityListBoResp();
        TeachingClassIdPoReq teachingClassIdPoReq = CommonUtil.objToObj(teachingClassIdBoReq, TeachingClassIdPoReq.class);
        //获得活动列表,包括测试(type=1) 和 任务(type=2),
        List<TeachingActivityPoResp> activitiesByTeachingClassId = teachingActivityDao.getActivitiesByTeachingClassId(teachingClassIdPoReq);
        activitiesByTeachingClassId.forEach(teachingActivityPoResp -> {
            //po转bo
            TeachingActivityBoResp teachingActivityBoResp = CommonUtil.objToObj(teachingActivityPoResp, TeachingActivityBoResp.class);
            if(teachingActivityPoResp.getState()==0){
                teachingActivityBoResp.setState(ActivityState.STATE_NO_PUBLISH);

            }else if(teachingActivityPoResp.getState()==1){
                teachingActivityBoResp.setState(ActivityState.STATE_RUNNING);
            }else if(teachingActivityPoResp.getState()==2){
                teachingActivityBoResp.setState(ActivityState.STATE_FINISH);
            }
            //将活动添加到集合
            teachingActivityListBoResp.getTeachingActivityList().add(teachingActivityBoResp);

        });

        return teachingActivityListBoResp;
    }

    /**
     * 根据教学活动编号,获取活动详情
     *
     * @param activityIdBoReq
     * @return
     */
    @Override
    public TeachingActivityDetailBoResp getTeachingActivityDetailByActivityId(ActivityIdBoReq activityIdBoReq) {
        return null;
    }

    /**
     * 根据活动编号,删除活动
     *
     * @param activityIdBoReq
     * @return
     */
    @Transactional
    @Override
    public CommonBoResp deleteActivityByActivityId(ActivityIdBoReq activityIdBoReq) throws SQLException {
        //ToDo 查询是否已经发布
        CommonPoResp commonPoResp =  new CommonPoResp();
        if(activityIdBoReq.getType() == 1){
            //查询是否正在进行中
            //删除
            int  update= testDao.deleteTestByTestId(activityIdBoReq.getActivityId());
            CommonUtil.printToPoResult(commonPoResp,update);

        }else if(activityIdBoReq.getType() == 2){

            int  update=  taskDao.deleteTaskByTaskId(activityIdBoReq.getActivityId());
            CommonUtil.printToPoResult(commonPoResp,update);
        }
        CommonBoResp commonBoResp  = CommonUtil.objToObj(commonPoResp, CommonBoResp.class);
        return commonBoResp;
    }

    /**
     * 更新活动状态,包括暂存后点击发布功能
     * @param activityStateBoReq
     * @return
     * @throws SQLException
     */
    @Transactional
    @Override
    public CommonBoResp updateActivityState(ActivityStateBoReq activityStateBoReq) throws SQLException {
        CommonPoResp commonPoResp =   new CommonPoResp();
        if(activityStateBoReq.getType() == 1){
            TestStatePoReq testStatePoReq = new TestStatePoReq();
            testStatePoReq.setState(activityStateBoReq.getState());
            testStatePoReq.setTestId(activityStateBoReq.getActivityId());
            //更改测试状态
            int  update=  testDao.updateTestState(testStatePoReq);
            CommonUtil.printToPoResult(commonPoResp,update);
        }else if(activityStateBoReq.getType() == 2){
            //更改任务状态
            TaskStatePoReq taskStatePoReq=new TaskStatePoReq();
            taskStatePoReq.setTaskId(activityStateBoReq.getActivityId());
            taskStatePoReq.setState(activityStateBoReq.getState());
            int  update= taskDao.updateTaskState(taskStatePoReq);
            CommonUtil.printToPoResult(commonPoResp,update);
        }
        CommonBoResp commonBoResp  = CommonUtil.objToObj(commonPoResp, CommonBoResp.class);
        Map<String,Object> logData =new HashMap<>();
        logData.put("方法名","updateActivityState");
        logData.put("请求数据",activityStateBoReq);
        logData.put("返回结果",commonBoResp);
        LogWriterUtil.write(log,logData);
        return commonBoResp;
    }
}
